package com.vcar.LDiceClient.loader
{
    //import com.puremvc.ApplicationFacade;

    import com.vcar.LDiceClient.config.GlobalEventName;
    import com.vcar.LDiceClient.config.MediatorName;
    
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.utils.setTimeout;
    
    import mx.events.*;
    import mx.preloaders.IPreloaderDisplay;
    
    import org.puremvc.as3.patterns.facade.Facade;

    /**
     * 主程序的预加载界面，一般来说flex程有两帧，preload与主场景，下载时，preload这一帧先下载，所以这一帧要尽量小
     * @author rhj
     *
     */    
    public class Preloader extends Sprite implements IPreloaderDisplay
    {
        private var soReady:Boolean = false;
        private var appReady:Boolean = false;

        private var _backgroundAlpha:Number = 1;
        private var _backgroundColor:uint = 0xffffff;
        private var _backgroundImage:Object;
        private var _backgroundSize:String = "";
        private var _stageHeight:Number = 600;
        private var _stageWidth:Number = 980;

        //一个可循环播放的动画，不应有太复杂的脚本在里面
//        [Embed(source='/res/swf/loading.swf')]
//        private var bgImage:Class;

        private var progBar:DisplayObject;
        private var border:Sprite=new Sprite;
        private var pl:Sprite;
//        private var swfLoader:myloader;
        private var buildCompleteBol:Boolean=false;
        private var loadProcess:int=0;
        public function Preloader()
        {
            super();
            var facade:ApplicationFacade=ApplicationFacade.getInstance();
            facade.sendNotification(GlobalEventName.SYS_INIT_START,this);

//            swfLoader = new myloader();
//            swfLoader.x=0;
//            swfLoader.y=0;
//            addChild(swfLoader);

            this.addEventListener(Event.REMOVED_FROM_STAGE,onRemove);

        }

        private function setCenter(obj:DisplayObject,width:Number, height:Number):void
        {
            obj.x = Math.floor((width - obj.width) / 2);
            obj.y = Math.floor((height - obj.height) / 2);
        }


        private function onRemove(e:Event):void
        {
            Facade.getInstance().removeMediator(MediatorName.PreloaderMediator);
        }

        public function get backgroundAlpha():Number
        {
            return _backgroundAlpha;
        }
        public function set backgroundAlpha(value:Number):void
        {
            _backgroundAlpha = value;
        }

        public function get backgroundColor():uint
        {
            return _backgroundColor;
        }
        public function set backgroundColor(value:uint):void
        {
            _backgroundColor = value;
        }

        public function get backgroundImage():Object
        {
            return _backgroundImage;
        }
        public function set backgroundImage(value:Object):void
        {
            _backgroundImage = value;
        }

        public function get backgroundSize():String
        {
            return _backgroundSize;
        }
        public function set backgroundSize(value:String):void
        {
            _backgroundSize = value;
        }

        public function get stageHeight():Number
        {
            return _stageHeight;
        }
        public function set stageHeight(value:Number):void
        {
            _stageHeight = value;
        }

        public function get stageWidth():Number
        {
            return _stageWidth;
        }
        public function set stageWidth(value:Number):void
        {
            _stageWidth = value;
        }

        public function initialize():void
        {
        }

        public function set preloader(pl:Sprite):void
        {
            pl.addEventListener( ProgressEvent.PROGRESS , SWFDownloadProgress );    
            pl.addEventListener( Event.COMPLETE , SWFDownloadComplete );
            this.pl = pl;
        }

        /**
         * 主文件下完，发布complete事件来切换到主场景，
         * 在实际项目中应该是加载完所有的必须swf
         * @param event
         *
         */        
        private function SWFDownloadComplete( event:Event ):void 
        {
			setTimeout(function ():void{
				dispatchEvent( new Event( Event.COMPLETE ));
			},1000);
			
        }
		
		/**
		 * 下载进度监听
		 * @auth 何伟章 
		 * @param event
		 * 
		 */		
		private function SWFDownloadProgress( event:ProgressEvent ):void 
		{
			
		}
		

    }
}

